package 笔试.面试手撕;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * @author:谢君臣
 * @Date:2021/9/4-22:22
 * @version:1.0
 * @function:
 */
public class LRU实现 {
    public static void main(String[] args) {
        int[][] test = new int[][]{{1, 1, 1}, {1, 2, 2}, {1, 3, 2}, {2, 1}, {1, 4, 4}, {2, 2}};
        LRU(test, 3);

    }

    public static int[] LRU(int[][] operators, int k) {
        // write code here
        Lru lruNode = new Lru(k);
//        lruNode.setCapacity(k);
        List<Integer> res = new ArrayList<>();
        for (int[] operator : operators) {
            if (operator[0] == 1) {
                String key = String.valueOf(operator[1]);
                int value = operator[2];
                lruNode.put(key, value);
            } else if (operator[0] == 2) {
                String key = String.valueOf(operator[1]);
                int value = lruNode.get(key);
                res.add(value);
            } else {
                System.out.println("异常输入");
            }
        }
        int[] r = new int[res.size()];
        Iterator<Integer> iterator = res.iterator();
        int i = 0;
        while (iterator.hasNext()) {
            r[i++] = iterator.next();
        }
        return r;
    }

}
class Lru{

    class node{
        public String key;
        public int value;
        public node next;
        public node pre;

        public node(String key, int value) {
            this.key = key;
            this.value = value;
        }
    }
    private final int DEFAULT_SIZE = 10;
    private int capacity;
    private HashMap<String,node> hashMap;
    private node head;
    private node tail;

    public Lru() {
        init(DEFAULT_SIZE);
    }

    private void init(int capacity) {
        this.capacity = capacity;
        this.hashMap = new HashMap<>();
    }

    public Lru(int capacity) {
        init(capacity);
    }

    public void put(String key,int value){
        node node = new node(key, value);
        // 当前队列为空
        if(head==null){
            head = node;
            tail = node;
        }else{
            //放置在头节点
            node.next = head;
            head.pre = node;
            head = node;
        }
        hashMap.put(key,node);
        if (hashMap.size()>capacity){  //溢出最后一个
            hashMap.remove(tail.key,tail);
            tail = tail.pre;
            tail.next.pre = null;
            tail.next = null;
        }
    }
    public int get(String key){
        int val = -1;
        if (hashMap.containsKey(key))
        {
            node node = hashMap.get(key);
            val = node.value;
            if (node!=head){
                if (node==tail){
                    tail = tail.pre;
                    tail.next.pre = null;
                    tail.next = null;
                }else {
                    node.pre.next = node.next;
                    node.next.pre = node.pre;
                    node.next = null;
                    node.pre = null;
                }
                node.next = head;
                head.pre = node;
                head = node;
            }
        }
        return val;
    }
}